package me.zhengjie.config;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.http.DefaultHttpClientBuilder;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.net.InetSocketAddress;
import java.net.Proxy;

@Configuration
@Data
public class WechatPayConfig {

    @Value("${wechat.pay.app-id}")
    private String appId;

    @Value("${wechat.pay.mch-id}")
    private String mchId;

    @Value("${wechat.pay.api-v3-key}")
    private String apiV3Key;

    @Value("${wechat.pay.private-key-path}")
    private String privateKeyPath;


    @Value("${wechat.pay.cert-serial-no}")
    private String certSerialNo;

    @Value("${wechat.pay.notify-url}")
    private String notifyUrl;

    @Value("${wechat.pay.refund-notify-url}")
    private String refundNotifyUrl;

    @Value("${wechat.pay.amount}")
    private Integer amount;

    @Value("${wechat.pay.hostname}")
    private String hostname;

    @Value("${wechat.pay.port}")
    private Integer port;

    /**
     * 根据商户号动态加载配置
     */
    public Config getNotificationConfig() {
        DefaultHttpClientBuilder clientBuilder =
                new DefaultHttpClientBuilder()
                        .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostname, port)));

        // 使用微信支付公钥的RSA配置
        return new RSAAutoCertificateConfig.Builder()
                .merchantId(mchId)
                .privateKeyFromPath(privateKeyPath)
                .merchantSerialNumber(certSerialNo)
                .apiV3Key(apiV3Key)
                .httpClientBuilder(clientBuilder)
                .build();
    }

//    public Config getHttpNotificationConfig() {
//        DefaultHttpClientBuilder clientBuilder =
//                new DefaultHttpClientBuilder()
//                        .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("https://192.168.10.23", 18445)));
//
//        // 使用微信支付公钥的RSA配置
//        return new RSAAutoCertificateConfig.Builder()
//                .merchantId(mchId)
//                .privateKeyFromPath(privateKeyPath)
//                .merchantSerialNumber(certSerialNo)
//                .apiV3Key(apiV3Key)
//                .httpClientBuilder(clientBuilder)
//                .build();
//    }

}
